package com.smartstate.ems.schedul;

import com.smartstate.ems.mapper.EquipmentMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@Component
public class RandomRedisValue {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private EquipmentMapper equipmentMapper;

    private DecimalFormat decimalFormat = new DecimalFormat("0.0");

    public void realTime(){
        List<Map<String, Object>> mapList = equipmentMapper.queryDeviceId("电表");
        for (Map<String, Object> map : mapList) {
            String deviceId = String.valueOf(map.get("deviceId"));
            if (redisTemplate.hasKey(deviceId)) {
                String apparentEnergyValue = decimalFormat.format(new BigDecimal(Math.random() * 500).setScale(1, RoundingMode.DOWN).floatValue());
                String reactiveEnergyValue = decimalFormat.format(new BigDecimal(Math.random() * 500).setScale(1, RoundingMode.DOWN).floatValue());
                String activeEnergyValue = decimalFormat.format(new BigDecimal(Math.random() * 500).setScale(1, RoundingMode.DOWN).floatValue());
                redisTemplate.opsForHash().put(deviceId, "apparentEnergy", apparentEnergyValue);
                redisTemplate.opsForHash().put(deviceId, "reactiveEnergy", reactiveEnergyValue);
                redisTemplate.opsForHash().put(deviceId, "activeEnergy", activeEnergyValue);
                redisTemplate.opsForHash().put(deviceId, "type", "正常");
            }
        }
    }


}
